package samsung.sds.survey.surveyVote.controller;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import samsung.sds.survey.surveyVote.model.QuestionEntity;
import samsung.sds.survey.surveyVote.model.SurveyEntity;
import samsung.sds.survey.surveyVote.service.VoteQuestionService;
import samsung.sds.survey.surveyVote.service.VoteSurveyResultService;
import samsung.sds.survey.surveyVote.service.VoteSurveyService;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;
import samsung.sds.system.utils.Constants;

/**
 * 
 * 获取投票页面的信息
 * 
 * @author lixin.qiu
 * @createDate 2016-01-18
 * 
 */
@Controller
@RequestMapping("/survey/surveyVote")
public class SurveyVoteController {

	// 获取问题、问题选项及投票结果
	@Autowired
	private VoteSurveyResultService voteSurveyResultServiceImpl;

	@Autowired
	private VoteSurveyService voteSurveyServiceImpl;

	@Autowired
	private VoteQuestionService voteQuestionServiceImpl;

	/**
	 * 待投票
	 * 
	 * @param modelMap
	 * @param surveyId
	 * @return
	 */
	@RequestMapping(value = "/viewSurvey", method = RequestMethod.POST)
	public String viewSurvey(ModelMap modelMap,HttpServletRequest request) {
		QueryParam param = CommonTools.getQueryParam(request);
		String surveyId = request.getParameter("surveyId");
		List<QuestionEntity> qList = voteQuestionServiceImpl.queryListWithDefault(param);
		SurveyEntity survey = voteSurveyServiceImpl.findEntityById(surveyId);
		modelMap.addAttribute("list", qList);
		modelMap.addAttribute("survey", survey);
		return "/survey/surveyVote/surveyVote";
	}

	/**
	 * 已投票
	 * 
	 * @param modelMap
	 * @param surveyId
	 * @return
	 */
	@RequestMapping(value = "/answerSurvey", method = RequestMethod.POST)
	public String answerSurvey(ModelMap modelMap, HttpServletRequest request) {
		String surveyId = request.getParameter("surveyId");
		String type = request.getParameter("type");
		SurveyEntity survey = voteSurveyServiceImpl.findEntityById(surveyId);
		// 如果投票结束跳转到已过期页面,其他的跳转至已投票页面
		if ("2".equals(survey.getStatus())) {
			modelMap.addAttribute("thisType", "past");
		} else {
			modelMap.addAttribute("thisType", "alreadyVote");
		}
		QueryParam param = CommonTools.getQueryParam(request);
		// 最终返回给页面的list:包含问题 选项 及答案
		List<QuestionEntity> editLists = voteSurveyResultServiceImpl
				.queryQuestionAndSurveyResultList(param, false);
		modelMap.addAttribute("list", editLists);
		modelMap.addAttribute("survey", survey);
		modelMap.addAttribute("type", type);
		modelMap.addAttribute("currentPage", param.getCurrentPage());
		return "/survey/surveyVote/voteEdit";
	}

	/**
	 * 投票预览
	 * 
	 * @param modelMap
	 * @param surveyId
	 * @return
	 */
	@RequestMapping(value = "/previewSurvey", method = RequestMethod.POST)
	public String previewSurvey(ModelMap modelMap, HttpServletRequest request) {
		QueryParam param = CommonTools.getQueryParam(request);
		String surveyId = request.getParameter("surveyId");
		List<QuestionEntity> qList = voteQuestionServiceImpl.queryListWithDefault(param);
		SurveyEntity survey = voteSurveyServiceImpl.findEntityById(surveyId);
		modelMap.addAttribute("list", qList);
		modelMap.addAttribute("survey", survey);
		return "/survey/surveyVote/votePreview";
	}

	/**
	 * 插入未投票的投票信息
	 * 
	 * @param request
	 * @param modelMap
	 * @param formParas
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addVote", method = RequestMethod.POST)
	@ResponseBody
	public String addVoteInfo(ModelMap modelMap,
			@RequestParam(value = "formParas") String formParas)
			throws Exception {
		if (formParas == null || "".equals(formParas) || "{}".equals(formParas)) {
			return Constants.ERRMSG_PARAM_ERROR;
		}
		Map<String, String> paraMap = translateStringToMap(formParas);
		Set<Entry<String, String>> keSet = paraMap.entrySet();
		int insertCount = voteSurveyResultServiceImpl
				.iteratorInsertSurveyResult(keSet);
		modelMap.addAttribute("type", "alreadyVoted");
		if (insertCount == keSet.size()) {
			return Constants.SUCCESS_CODE;
		} else {
			return Constants.FAILURE_CODE;
		}
	}

	/**
	 * 更新已投票的投票信息
	 * 
	 * @param request
	 * @param modelMap
	 * @param formParas
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/updateVote", method = RequestMethod.POST)
	@ResponseBody
	public String updateVoteInfo(ModelMap modelMap,
			@RequestParam(value = "formParas") String formParas)
			throws Exception {
		if (formParas == null || "".equals(formParas) || "{}".equals(formParas)) {
			return Constants.ERRMSG_PARAM_ERROR;
		}
		Map<String, String> paraMap = translateStringToMap(formParas);
		Set<Entry<String, String>> keSet = paraMap.entrySet();
		int updateCount = voteSurveyResultServiceImpl
				.iteratorUpdateSurveyResult(keSet);
		modelMap.addAttribute("type", "alreadyVoted");
		if (updateCount == keSet.size()) {
			return Constants.SUCCESS_CODE;
		} else {
			return Constants.FAILURE_CODE;
		}
	}

	/**
	 * 字符串参数转为Map
	 * 
	 * @param formParas
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private Map<String, String> translateStringToMap(String formParas)
			throws Exception {
		ObjectMapper objectMapper = new ObjectMapper();
		Map<String, String> paraMap = objectMapper.readValue(formParas,
				Map.class);
		return paraMap;
	}
}
